package com.currentbp.Interesting.likou.cannot;

import com.currentbp.util.all.StringUtil;
import org.junit.Test;

import java.util.*;

/**
 * @author baopan
 * @createTime 2020/8/5 22:11
 */
public class T00329LongestIncreasingPath {
    /*
给定一个整数矩阵，找出最长递增路径的长度。
对于每个单元格，你可以往上，下，左，右四个方向移动。
你不能在对角线方向上移动或移动到边界外（即不允许环绕）。
示例 1:
输入: nums =
[
  [9,9,4],
  [6,6,8],
  [2,1,1]
]
输出: 4
解释: 最长递增路径为 [1, 2, 6, 9]。
示例 2:
输入: nums =
[
  [3,4,5],
  [3,2,6],
  [2,2,1]
]
输出: 4
解释: 最长递增路径是 [3, 4, 5, 6]。注意不允许在对角线方向上移动。

解题思路：
1、按照上左下右的顺序搜索该点能到的目的地，只要下一个值是比自己要大的值就是ok的，并记录到list中，直到所有点都比自己小
2、用一个map<String,String>记录这个有向图，然后遍历每一个点，判断这个点的最大长度
     */

    @Test
    public void t3() {
        int[][] a = new int[][]{
                {3, 4, 5},
                {3, 2, 6},
                {2, 2, 1}};

//        StringUtil.printObject(a[0]);//y=0
        int i = longestIncreasingPath2(a);
        StringUtil.printObject(i);
    }

    @Test
    public void t1() {
        int[][] a = new int[][]{
                {1}};

//        StringUtil.printObject(a[0]);//y=0
        int i = longestIncreasingPath(a);
        StringUtil.printObject(i);
    }

    @Test
    public void t2() {
        int[][] a = new int[][]{{70, 48, 80, 57, 74, 47, 57, 92, 77, 71, 2, 40, 1, 28, 83, 41, 5, 61, 18, 38, 38, 66, 43, 55, 44, 42, 90, 90, 54, 90, 22, 87, 30, 30, 74, 13, 16, 57, 52, 38, 38, 15, 35, 46, 8, 63, 51, 97, 45, 39, 63, 95, 82, 94}, {21, 86, 61, 76, 78, 63, 67, 33, 58, 14, 4, 79, 59, 76, 95, 6, 23, 22, 80, 12, 99, 44, 21, 70, 67, 72, 81, 54, 97, 26, 6, 77, 93, 73, 39, 69, 43, 4, 74, 80, 99, 10, 3, 81, 35, 79, 24, 49, 32, 49, 57, 65, 3, 72}, {33, 79, 27, 70, 84, 79, 59, 23, 85, 89, 89, 88, 8, 44, 10, 95, 4, 65, 31, 67, 7, 14, 45, 79, 82, 50, 85, 22, 36, 47, 86, 92, 36, 60, 67, 60, 31, 13, 26, 33, 0, 68, 67, 52, 26, 58, 91, 49, 32, 75, 84, 78, 9, 59}, {30, 78, 75, 98, 12, 69, 62, 54, 73, 85, 86, 32, 3, 1, 21, 37, 98, 42, 51, 48, 37, 52, 92, 10, 9, 83, 21, 66, 79, 99, 15, 27, 53, 1, 25, 15, 59, 99, 22, 93, 40, 24, 58, 27, 43, 18, 58, 43, 18, 16, 86, 32, 13, 13}, {19, 70, 41, 63, 86, 58, 9, 29, 0, 27, 83, 36, 41, 59, 4, 36, 72, 21, 10, 59, 5, 22, 23, 18, 34, 74, 82, 93, 48, 97, 66, 94, 69, 71, 62, 74, 84, 76, 8, 84, 85, 99, 7, 54, 95, 73, 89, 46, 60, 14, 79, 46, 83, 43}, {75, 97, 69, 91, 67, 99, 22, 37, 79, 63, 56, 44, 9, 40, 15, 70, 46, 33, 48, 98, 10, 57, 99, 36, 81, 59, 7, 56, 45, 91, 80, 12, 71, 40, 28, 90, 93, 99, 56, 81, 22, 41, 28, 71, 42, 4, 34, 90, 94, 71, 40, 93, 4, 20}, {84, 63, 39, 47, 80, 46, 41, 65, 95, 11, 91, 58, 70, 18, 63, 81, 46, 62, 78, 67, 87, 97, 28, 40, 3, 35, 95, 26, 41, 8, 99, 74, 67, 79, 21, 21, 39, 20, 9, 60, 33, 18, 19, 76, 88, 24, 34, 21, 16, 39, 26, 42, 11, 98}, {43, 11, 40, 9, 99, 39, 75, 74, 30, 9, 0, 17, 42, 41, 64, 68, 81, 11, 50, 84, 15, 53, 24, 4, 25, 23, 33, 96, 84, 33, 51, 37, 64, 44, 6, 4, 69, 13, 3, 48, 43, 27, 19, 50, 18, 72, 56, 28, 92, 48, 2, 78, 67, 42}, {84, 93, 41, 11, 78, 29, 59, 77, 28, 75, 34, 81, 96, 76, 40, 21, 32, 13, 66, 37, 96, 69, 17, 17, 26, 36, 15, 17, 13, 14, 16, 10, 24, 48, 37, 29, 15, 58, 11, 64, 80, 39, 98, 72, 8, 66, 66, 6, 11, 15, 80, 64, 46, 97}, {98, 97, 95, 42, 33, 87, 30, 6, 56, 21, 66, 73, 62, 88, 44, 80, 60, 97, 81, 70, 28, 54, 24, 56, 42, 49, 74, 72, 25, 62, 26, 85, 51, 3, 41, 84, 51, 26, 58, 40, 72, 40, 24, 39, 71, 94, 52, 6, 74, 17, 11, 92, 27, 23}, {17, 16, 28, 16, 83, 20, 56, 24, 44, 54, 67, 91, 66, 70, 68, 51, 29, 98, 40, 6, 37, 99, 89, 61, 85, 31, 41, 10, 12, 17, 40, 35, 75, 89, 19, 46, 77, 93, 30, 30, 52, 25, 82, 70, 89, 37, 1, 20, 43, 79, 61, 59, 49, 92}, {79, 35, 29, 33, 65, 80, 6, 48, 14, 62, 38, 79, 33, 12, 41, 64, 98, 99, 13, 71, 40, 55, 71, 1, 94, 21, 56, 15, 77, 0, 25, 39, 62, 34, 1, 63, 62, 94, 8, 93, 45, 20, 13, 54, 9, 1, 54, 4, 80, 81, 36, 30, 65, 19}, {18, 86, 34, 20, 27, 71, 46, 4, 92, 40, 2, 43, 60, 14, 24, 56, 86, 78, 7, 45, 1, 67, 79, 38, 8, 23, 94, 39, 53, 4, 17, 85, 60, 7, 73, 8, 33, 1, 22, 64, 82, 7, 78, 6, 76, 24, 19, 3, 65, 85, 13, 48, 98, 44}, {39, 81, 99, 38, 53, 36, 4, 8, 68, 93, 13, 21, 28, 89, 18, 55, 79, 87, 40, 91, 34, 91, 59, 15, 35, 14, 48, 71, 78, 75, 60, 75, 5, 1, 40, 89, 92, 2, 49, 70, 43, 4, 11, 76, 3, 42, 65, 92, 91, 58, 24, 57, 11, 98}, {54, 69, 92, 5, 71, 71, 91, 88, 91, 47, 13, 79, 42, 59, 45, 67, 23, 62, 24, 93, 76, 87, 85, 53, 20, 40, 68, 88, 56, 96, 5, 83, 57, 20, 90, 45, 99, 2, 11, 45, 94, 4, 47, 2, 21, 93, 52, 16, 87, 5, 96, 77, 63, 71}, {70, 24, 77, 19, 32, 57, 72, 38, 59, 81, 23, 15, 5, 78, 50, 64, 8, 16, 6, 58, 71, 11, 77, 98, 38, 9, 89, 86, 47, 23, 29, 87, 7, 24, 5, 1, 61, 34, 38, 40, 77, 6, 57, 14, 98, 46, 98, 96, 66, 34, 27, 90, 89, 61}, {47, 15, 82, 70, 65, 48, 41, 51, 28, 45, 56, 85, 0, 38, 65, 10, 92, 1, 33, 73, 11, 66, 15, 8, 15, 31, 26, 91, 45, 82, 49, 28, 78, 72, 89, 59, 86, 69, 11, 14, 10, 18, 44, 7, 81, 31, 38, 10, 65, 75, 92, 69, 33, 33}, {21, 49, 91, 93, 39, 63, 7, 52, 36, 69, 10, 29, 3, 44, 33, 24, 27, 67, 51, 81, 68, 62, 50, 59, 31, 49, 34, 27, 24, 43, 3, 81, 77, 7, 32, 49, 34, 90, 30, 15, 4, 43, 60, 37, 3, 57, 34, 70, 38, 15, 9, 96, 43, 94}, {93, 6, 59, 71, 26, 84, 97, 83, 48, 25, 69, 21, 5, 20, 55, 20, 54, 47, 46, 35, 51, 44, 57, 18, 33, 49, 69, 61, 30, 68, 58, 83, 71, 40, 40, 5, 89, 19, 83, 86, 58, 29, 62, 52, 59, 62, 29, 69, 51, 28, 6, 20, 75, 72}, {98, 70, 82, 54, 2, 25, 41, 12, 68, 41, 19, 57, 41, 78, 55, 0, 20, 78, 79, 73, 1, 31, 57, 86, 49, 93, 60, 86, 77, 62, 68, 23, 14, 47, 64, 90, 27, 52, 18, 82, 94, 23, 3, 5, 66, 51, 88, 99, 39, 56, 47, 29, 68, 71}, {79, 45, 58, 19, 35, 69, 89, 26, 35, 22, 64, 91, 36, 80, 54, 14, 94, 47, 24, 91, 89, 77, 75, 23, 21, 35, 18, 39, 64, 34, 53, 15, 89, 87, 88, 12, 66, 22, 39, 83, 51, 44, 92, 23, 90, 4, 5, 81, 20, 70, 44, 11, 98, 8}, {16, 46, 13, 20, 21, 5, 0, 34, 82, 95, 64, 99, 41, 57, 62, 80, 12, 38, 88, 2, 40, 95, 10, 62, 74, 39, 36, 41, 1, 24, 6, 9, 84, 9, 5, 84, 25, 13, 52, 35, 57, 20, 90, 10, 34, 68, 27, 35, 26, 42, 7, 44, 89, 88}, {84, 59, 59, 52, 51, 21, 25, 55, 0, 74, 81, 43, 52, 53, 5, 50, 68, 0, 84, 33, 86, 35, 49, 2, 60, 11, 43, 75, 79, 57, 58, 68, 99, 45, 2, 79, 44, 25, 32, 14, 46, 18, 14, 85, 50, 20, 78, 26, 28, 33, 45, 99, 13, 24}, {18, 79, 26, 48, 37, 66, 76, 18, 80, 20, 12, 2, 73, 12, 71, 70, 11, 30, 54, 42, 7, 66, 83, 8, 44, 83, 47, 82, 87, 1, 97, 99, 85, 87, 49, 66, 90, 34, 78, 55, 90, 91, 95, 28, 98, 72, 72, 36, 6, 43, 57, 20, 60, 6}, {63, 70, 90, 46, 83, 91, 53, 27, 13, 60, 52, 74, 65, 86, 28, 72, 22, 71, 82, 63, 55, 13, 35, 66, 24, 90, 60, 32, 99, 26, 10, 42, 11, 98, 49, 12, 62, 85, 49, 62, 81, 24, 34, 24, 6, 59, 5, 39, 35, 63, 20, 39, 34, 69}, {95, 43, 61, 65, 94, 31, 67, 23, 43, 97, 54, 99, 52, 95, 8, 67, 34, 17, 46, 71, 69, 37, 39, 68, 67, 55, 89, 51, 9, 98, 14, 42, 46, 25, 34, 21, 43, 87, 98, 91, 8, 96, 81, 50, 99, 48, 59, 89, 55, 29, 46, 81, 37, 28}, {89, 7, 4, 5, 77, 14, 16, 86, 36, 37, 43, 51, 40, 37, 92, 35, 24, 26, 45, 26, 2, 55, 80, 19, 5, 7, 98, 69, 80, 89, 55, 27, 16, 96, 55, 85, 6, 77, 26, 93, 4, 83, 68, 55, 29, 94, 48, 21, 81, 76, 82, 19, 79, 81}, {92, 23, 32, 21, 50, 94, 1, 16, 6, 60, 24, 33, 83, 17, 76, 64, 67, 88, 85, 35, 88, 41, 0, 48, 80, 55, 29, 89, 27, 8, 16, 65, 86, 82, 45, 7, 3, 54, 71, 13, 71, 84, 28, 55, 62, 7, 15, 38, 82, 99, 33, 40, 51, 37}, {9, 34, 87, 54, 71, 6, 77, 85, 31, 30, 27, 51, 27, 57, 37, 44, 33, 5, 18, 32, 54, 23, 19, 63, 92, 24, 43, 15, 38, 32, 33, 84, 25, 13, 82, 47, 36, 14, 42, 44, 34, 74, 10, 73, 1, 92, 52, 41, 95, 36, 6, 78, 36, 48}, {14, 64, 10, 58, 67, 22, 89, 35, 18, 58, 39, 49, 16, 52, 12, 40, 75, 31, 20, 23, 33, 18, 94, 15, 96, 11, 93, 94, 60, 67, 55, 6, 59, 86, 57, 11, 58, 40, 77, 59, 42, 33, 35, 81, 98, 6, 79, 74, 53, 2, 9, 61, 12, 56}, {39, 94, 77, 6, 52, 14, 83, 47, 82, 57, 13, 30, 89, 12, 99, 51, 72, 38, 13, 80, 49, 70, 27, 81, 77, 4, 42, 16, 10, 69, 76, 16, 20, 29, 3, 85, 71, 57, 4, 82, 51, 89, 94, 51, 87, 84, 91, 18, 56, 70, 39, 44, 2, 52}, {48, 16, 37, 36, 43, 33, 78, 91, 28, 62, 28, 56, 62, 80, 75, 62, 70, 59, 88, 59, 97, 1, 66, 69, 85, 57, 10, 1, 52, 63, 9, 6, 41, 37, 12, 59, 65, 74, 49, 3, 65, 67, 1, 84, 22, 64, 86, 14, 65, 76, 66, 9, 82, 17}, {95, 28, 38, 43, 6, 6, 72, 65, 66, 25, 21, 47, 30, 6, 10, 18, 24, 20, 39, 65, 49, 99, 83, 96, 2, 49, 23, 32, 36, 61, 29, 7, 40, 32, 85, 29, 79, 13, 11, 64, 86, 64, 68, 85, 26, 68, 64, 38, 57, 59, 33, 28, 65, 43}, {97, 48, 42, 26, 10, 34, 12, 98, 90, 21, 27, 23, 47, 92, 79, 82, 41, 75, 68, 37, 39, 77, 22, 73, 81, 73, 70, 19, 43, 38, 29, 79, 76, 93, 67, 26, 74, 72, 32, 36, 85, 78, 91, 6, 15, 70, 84, 18, 42, 75, 52, 81, 34, 1}, {56, 41, 23, 19, 59, 86, 15, 82, 96, 23, 80, 79, 4, 77, 92, 44, 49, 47, 86, 91, 65, 36, 2, 20, 67, 88, 95, 98, 94, 83, 0, 78, 33, 36, 52, 65, 22, 1, 66, 23, 75, 91, 94, 52, 93, 38, 91, 15, 19, 47, 74, 22, 32, 67}, {15, 94, 51, 83, 82, 8, 27, 65, 58, 96, 77, 15, 22, 18, 93, 99, 2, 31, 95, 43, 88, 43, 54, 46, 20, 69, 26, 13, 15, 98, 64, 84, 87, 72, 74, 33, 24, 68, 42, 84, 51, 51, 75, 76, 72, 34, 31, 66, 47, 0, 97, 4, 95, 73}, {8, 81, 95, 90, 13, 91, 46, 76, 9, 42, 38, 42, 42, 97, 61, 42, 99, 0, 8, 31, 5, 81, 29, 22, 77, 11, 56, 4, 84, 16, 51, 40, 22, 14, 39, 94, 98, 47, 71, 83, 84, 91, 83, 12, 56, 32, 86, 22, 31, 32, 4, 40, 85, 53}, {99, 98, 62, 61, 18, 25, 51, 81, 85, 27, 16, 82, 96, 37, 91, 56, 82, 16, 95, 20, 38, 77, 64, 68, 17, 2, 45, 65, 60, 6, 72, 92, 73, 33, 34, 21, 76, 24, 85, 86, 15, 67, 98, 69, 59, 27, 40, 78, 84, 50, 25, 16, 12, 43}, {21, 22, 28, 83, 89, 78, 51, 97, 73, 94, 23, 91, 81, 3, 87, 37, 35, 51, 19, 19, 89, 42, 36, 99, 99, 90, 87, 9, 11, 52, 41, 57, 13, 79, 93, 19, 67, 16, 66, 68, 50, 12, 61, 0, 31, 18, 47, 0, 92, 85, 43, 8, 44, 74}, {62, 63, 54, 22, 20, 66, 11, 56, 16, 74, 2, 78, 77, 81, 4, 55, 92, 57, 1, 32, 86, 37, 49, 4, 18, 69, 74, 42, 29, 9, 59, 37, 89, 5, 74, 84, 0, 20, 84, 75, 6, 99, 9, 87, 69, 82, 63, 30, 38, 62, 71, 44, 8, 74}, {46, 38, 71, 60, 87, 48, 99, 34, 27, 58, 15, 62, 39, 31, 9, 16, 2, 91, 18, 68, 62, 50, 99, 65, 24, 94, 20, 45, 52, 63, 34, 86, 95, 15, 52, 24, 80, 29, 99, 56, 79, 78, 45, 95, 21, 62, 60, 81, 66, 1, 9, 41, 12, 40}, {81, 38, 15, 59, 60, 12, 3, 13, 48, 1, 67, 93, 97, 15, 89, 99, 87, 34, 7, 53, 46, 94, 91, 79, 50, 91, 14, 52, 74, 75, 56, 47, 10, 90, 85, 21, 37, 20, 18, 54, 66, 87, 66, 44, 63, 67, 62, 5, 9, 5, 3, 50, 79, 80}, {55, 98, 45, 19, 93, 66, 46, 39, 39, 37, 45, 1, 41, 95, 29, 9, 3, 14, 40, 69, 28, 10, 90, 30, 26, 88, 91, 84, 56, 62, 27, 33, 39, 49, 67, 17, 48, 99, 20, 52, 18, 16, 52, 65, 88, 45, 9, 3, 11, 25, 98, 11, 29, 17}, {97, 70, 55, 90, 91, 7, 69, 92, 51, 29, 11, 18, 58, 63, 41, 81, 37, 25, 88, 94, 30, 85, 16, 53, 40, 58, 77, 20, 36, 20, 2, 29, 8, 56, 35, 40, 0, 91, 50, 73, 34, 36, 13, 75, 52, 7, 39, 22, 40, 82, 83, 39, 38, 10}, {47, 25, 54, 1, 71, 50, 6, 4, 66, 8, 57, 84, 71, 79, 6, 47, 0, 53, 14, 73, 11, 94, 0, 29, 66, 18, 46, 2, 1, 25, 66, 94, 32, 73, 64, 90, 47, 7, 72, 64, 6, 80, 13, 22, 86, 94, 20, 56, 34, 52, 51, 54, 23, 14}, {33, 32, 2, 79, 39, 71, 56, 37, 5, 68, 52, 51, 30, 4, 50, 91, 3, 75, 76, 45, 62, 58, 93, 33, 8, 17, 60, 5, 82, 53, 95, 39, 21, 64, 68, 35, 70, 71, 52, 68, 3, 17, 66, 61, 7, 42, 88, 82, 69, 73, 6, 85, 80, 48}, {69, 4, 25, 31, 26, 25, 7, 36, 83, 22, 51, 30, 15, 26, 35, 19, 3, 49, 27, 8, 78, 15, 32, 85, 45, 77, 51, 75, 8, 54, 74, 84, 0, 47, 59, 28, 81, 27, 65, 1, 61, 53, 70, 5, 68, 77, 75, 47, 83, 47, 19, 2, 44, 15}, {56, 90, 20, 42, 37, 24, 60, 68, 69, 9, 94, 7, 98, 59, 8, 0, 96, 77, 79, 26, 96, 61, 93, 92, 18, 23, 53, 0, 94, 2, 33, 79, 72, 35, 6, 79, 80, 65, 36, 88, 61, 45, 41, 71, 70, 65, 55, 36, 1, 65, 26, 52, 32, 47}, {44, 43, 67, 23, 73, 88, 28, 28, 48, 55, 60, 93, 79, 23, 54, 17, 99, 46, 60, 22, 47, 58, 43, 12, 63, 87, 52, 87, 66, 99, 10, 54, 80, 21, 98, 3, 19, 17, 5, 93, 45, 73, 50, 72, 51, 54, 12, 77, 9, 58, 53, 78, 77, 86}, {73, 64, 22, 0, 36, 15, 43, 95, 86, 23, 90, 95, 8, 95, 46, 37, 53, 44, 4, 71, 65, 24, 21, 20, 51, 80, 83, 95, 58, 43, 19, 14, 16, 96, 30, 24, 42, 7, 0, 48, 4, 13, 13, 21, 53, 79, 56, 18, 68, 66, 66, 77, 43, 53}, {91, 41, 9, 34, 68, 74, 43, 54, 89, 75, 4, 40, 39, 90, 21, 98, 78, 78, 75, 5, 13, 58, 92, 62, 67, 6, 50, 26, 54, 16, 60, 22, 23, 86, 7, 72, 72, 23, 49, 15, 30, 51, 61, 73, 31, 75, 2, 68, 73, 10, 77, 94, 82, 91}, {54, 38, 71, 44, 19, 72, 56, 56, 13, 17, 74, 37, 78, 5, 60, 53, 8, 14, 91, 64, 99, 6, 4, 71, 60, 60, 61, 43, 97, 48, 21, 7, 6, 69, 66, 72, 79, 85, 77, 37, 86, 96, 67, 16, 16, 65, 46, 61, 79, 3, 0, 64, 6, 31}, {86, 49, 20, 28, 9, 24, 63, 52, 25, 39, 73, 73, 54, 38, 95, 92, 8, 54, 26, 13, 84, 14, 27, 49, 72, 42, 82, 74, 65, 1, 98, 9, 87, 82, 41, 30, 83, 43, 36, 25, 62, 87, 79, 40, 76, 43, 33, 50, 37, 0, 61, 18, 55, 63}, {58, 81, 6, 69, 90, 10, 77, 70, 79, 96, 13, 10, 44, 47, 89, 92, 34, 31, 56, 83, 46, 71, 64, 94, 82, 78, 36, 19, 73, 28, 9, 19, 60, 91, 38, 50, 63, 81, 42, 99, 62, 26, 35, 48, 60, 64, 72, 29, 30, 99, 12, 26, 97, 38}, {70, 34, 86, 4, 74, 82, 15, 36, 82, 3, 79, 22, 14, 41, 16, 41, 46, 73, 85, 10, 37, 13, 38, 11, 7, 84, 52, 37, 85, 97, 58, 93, 29, 5, 58, 29, 54, 62, 21, 28, 69, 73, 9, 35, 37, 89, 54, 0, 18, 30, 5, 89, 28, 33}, {22, 13, 20, 39, 92, 67, 99, 23, 5, 76, 75, 58, 56, 40, 32, 30, 35, 97, 11, 79, 48, 48, 76, 83, 96, 40, 29, 0, 34, 77, 3, 7, 63, 70, 73, 16, 6, 0, 27, 37, 67, 27, 62, 2, 73, 34, 25, 55, 8, 14, 53, 40, 47, 73}, {78, 82, 83, 94, 17, 30, 41, 81, 54, 28, 85, 33, 75, 2, 38, 73, 80, 55, 88, 61, 35, 71, 22, 70, 23, 29, 29, 41, 84, 96, 18, 92, 55, 55, 50, 26, 16, 84, 4, 26, 57, 88, 61, 0, 54, 45, 1, 28, 94, 61, 23, 36, 37, 14}, {61, 53, 9, 80, 70, 49, 75, 99, 44, 76, 67, 90, 46, 80, 15, 34, 37, 63, 5, 38, 3, 30, 53, 69, 33, 73, 66, 78, 84, 62, 97, 96, 15, 57, 24, 79, 49, 41, 69, 22, 39, 94, 22, 22, 66, 20, 50, 80, 98, 57, 37, 15, 10, 58}, {42, 7, 55, 50, 30, 22, 72, 5, 28, 84, 45, 85, 50, 78, 53, 27, 37, 95, 58, 16, 25, 32, 35, 46, 40, 54, 85, 83, 58, 83, 30, 46, 51, 33, 4, 66, 36, 47, 7, 43, 12, 46, 9, 6, 26, 91, 72, 29, 36, 72, 57, 54, 7, 12}, {97, 42, 66, 21, 12, 49, 82, 57, 33, 68, 21, 85, 18, 74, 72, 35, 92, 97, 5, 66, 29, 86, 40, 96, 22, 25, 62, 28, 68, 94, 85, 42, 3, 16, 73, 36, 77, 12, 99, 62, 92, 33, 75, 74, 3, 44, 92, 63, 80, 47, 77, 57, 83, 9}, {52, 46, 96, 5, 38, 31, 3, 83, 62, 97, 73, 11, 66, 11, 86, 27, 96, 34, 30, 6, 59, 36, 41, 20, 22, 70, 22, 81, 58, 49, 21, 6, 87, 52, 10, 6, 74, 37, 38, 82, 14, 65, 80, 1, 60, 6, 5, 11, 98, 36, 68, 57, 47, 23}, {42, 93, 37, 13, 75, 11, 25, 0, 94, 65, 28, 55, 63, 84, 64, 66, 13, 85, 45, 32, 76, 76, 82, 1, 92, 91, 79, 65, 57, 55, 12, 32, 24, 11, 69, 52, 74, 17, 62, 84, 9, 27, 99, 95, 27, 87, 13, 63, 24, 72, 3, 88, 48, 45}, {99, 8, 65, 19, 85, 60, 87, 44, 42, 25, 93, 14, 15, 78, 32, 83, 89, 39, 8, 59, 29, 47, 63, 43, 92, 70, 87, 90, 5, 1, 80, 92, 60, 12, 7, 60, 24, 59, 38, 71, 28, 96, 11, 60, 46, 96, 45, 49, 73, 45, 50, 78, 81, 68}, {99, 76, 91, 45, 66, 99, 14, 61, 41, 90, 81, 92, 30, 46, 62, 99, 27, 78, 72, 20, 92, 58, 10, 18, 23, 34, 59, 60, 47, 95, 27, 10, 38, 89, 16, 43, 93, 91, 43, 86, 47, 21, 17, 39, 25, 75, 61, 83, 17, 12, 72, 85, 58, 50}, {28, 59, 41, 24, 31, 13, 74, 58, 40, 32, 49, 4, 72, 28, 16, 16, 38, 52, 49, 1, 86, 95, 22, 49, 0, 25, 45, 48, 61, 87, 59, 63, 3, 40, 41, 77, 37, 61, 53, 8, 58, 95, 15, 75, 23, 72, 3, 23, 65, 41, 32, 28, 98, 36}, {8, 24, 39, 14, 95, 23, 23, 94, 10, 94, 3, 5, 23, 58, 13, 74, 3, 8, 3, 47, 13, 92, 69, 72, 3, 34, 64, 50, 35, 9, 62, 72, 11, 87, 27, 9, 54, 86, 33, 59, 35, 70, 8, 13, 36, 95, 2, 50, 60, 66, 95, 35, 22, 93}, {41, 38, 58, 16, 33, 78, 71, 16, 93, 56, 79, 10, 47, 0, 10, 51, 60, 65, 85, 39, 51, 37, 4, 32, 73, 32, 24, 23, 64, 76, 47, 15, 84, 84, 1, 30, 70, 79, 63, 52, 93, 5, 28, 21, 3, 4, 20, 35, 32, 27, 66, 38, 3, 16}, {38, 81, 46, 65, 19, 59, 40, 30, 15, 70, 58, 61, 89, 95, 78, 74, 60, 51, 80, 14, 86, 6, 56, 76, 40, 42, 37, 19, 81, 51, 48, 96, 15, 26, 16, 59, 81, 48, 14, 48, 59, 82, 39, 57, 48, 12, 65, 43, 46, 55, 99, 51, 16, 10}, {29, 2, 13, 60, 75, 88, 30, 24, 78, 14, 66, 51, 21, 92, 54, 33, 79, 79, 54, 83, 30, 32, 7, 67, 76, 77, 4, 1, 22, 40, 77, 3, 30, 55, 20, 89, 97, 79, 89, 87, 10, 49, 35, 40, 15, 58, 30, 3, 58, 31, 1, 85, 85, 74}, {51, 43, 58, 57, 46, 93, 37, 77, 38, 24, 47, 46, 84, 1, 93, 74, 38, 28, 11, 3, 16, 44, 95, 67, 73, 40, 43, 51, 8, 87, 22, 96, 50, 41, 7, 86, 81, 7, 75, 74, 84, 82, 89, 68, 10, 55, 16, 43, 65, 22, 74, 21, 30, 65}, {65, 82, 7, 68, 34, 68, 74, 99, 9, 81, 30, 88, 64, 14, 48, 35, 17, 40, 91, 8, 23, 5, 93, 37, 17, 68, 78, 57, 77, 39, 60, 21, 68, 15, 32, 61, 83, 42, 63, 96, 4, 48, 45, 37, 75, 95, 13, 45, 11, 98, 31, 91, 89, 24}, {42, 41, 51, 19, 77, 82, 66, 12, 3, 18, 6, 10, 0, 26, 43, 80, 40, 49, 34, 99, 97, 62, 6, 53, 89, 97, 37, 40, 32, 85, 79, 74, 4, 89, 93, 19, 58, 84, 96, 69, 50, 94, 61, 24, 23, 72, 65, 59, 93, 83, 58, 89, 24, 38}, {17, 81, 6, 11, 58, 98, 90, 37, 22, 83, 64, 39, 48, 32, 1, 96, 60, 49, 63, 90, 22, 55, 41, 21, 2, 70, 9, 55, 69, 31, 20, 58, 2, 87, 44, 50, 64, 20, 45, 41, 45, 62, 45, 44, 44, 27, 62, 85, 33, 85, 42, 38, 57, 17}, {23, 45, 69, 62, 49, 3, 54, 59, 23, 21, 87, 31, 50, 60, 41, 70, 10, 1, 57, 40, 12, 20, 77, 30, 37, 59, 27, 85, 44, 10, 85, 61, 67, 87, 94, 59, 20, 4, 11, 55, 83, 12, 79, 4, 93, 7, 46, 61, 15, 45, 20, 73, 69, 35}, {50, 31, 74, 31, 50, 77, 28, 50, 7, 91, 50, 39, 49, 77, 51, 54, 78, 9, 19, 30, 87, 6, 80, 76, 50, 19, 97, 38, 72, 72, 75, 65, 4, 61, 33, 16, 99, 12, 59, 62, 71, 79, 73, 70, 94, 63, 53, 25, 12, 95, 79, 51, 42, 15}, {28, 96, 83, 42, 73, 20, 40, 18, 50, 98, 47, 72, 59, 55, 52, 66, 37, 37, 57, 22, 38, 27, 70, 98, 3, 68, 81, 4, 98, 88, 88, 93, 92, 33, 59, 33, 29, 25, 77, 91, 87, 26, 59, 60, 78, 31, 16, 49, 89, 77, 89, 98, 98, 82}, {0, 97, 44, 1, 53, 5, 74, 92, 61, 37, 93, 80, 95, 16, 11, 79, 69, 22, 66, 68, 76, 9, 55, 0, 8, 75, 84, 0, 22, 73, 70, 60, 41, 89, 17, 71, 95, 36, 74, 92, 17, 90, 98, 77, 11, 48, 49, 46, 6, 76, 34, 38, 29, 4}, {55, 59, 50, 29, 53, 76, 9, 94, 68, 4, 83, 38, 61, 45, 89, 39, 2, 71, 21, 13, 20, 56, 22, 43, 40, 73, 4, 57, 8, 10, 7, 99, 36, 79, 53, 94, 96, 55, 34, 81, 74, 19, 64, 8, 41, 29, 89, 88, 78, 1, 80, 34, 54, 76}, {18, 11, 5, 92, 25, 92, 11, 98, 46, 68, 43, 93, 33, 95, 39, 38, 29, 30, 94, 64, 36, 46, 49, 53, 92, 81, 94, 3, 23, 12, 37, 39, 77, 90, 31, 60, 23, 26, 55, 48, 70, 46, 29, 92, 11, 72, 7, 17, 5, 37, 82, 60, 61, 72}, {16, 38, 17, 77, 54, 4, 1, 68, 41, 80, 72, 62, 85, 44, 97, 55, 50, 53, 70, 9, 16, 52, 61, 36, 87, 16, 76, 91, 89, 9, 53, 0, 26, 48, 33, 92, 85, 89, 70, 90, 89, 98, 62, 94, 45, 65, 20, 78, 1, 17, 91, 49, 50, 50}, {58, 12, 28, 71, 88, 43, 58, 26, 83, 80, 76, 99, 9, 94, 25, 89, 22, 37, 73, 26, 17, 26, 13, 86, 67, 24, 3, 4, 89, 91, 5, 57, 27, 0, 93, 74, 38, 96, 43, 68, 63, 57, 18, 15, 42, 96, 90, 12, 37, 50, 15, 9, 95, 35}, {7, 35, 34, 52, 2, 66, 8, 81, 47, 94, 22, 79, 35, 81, 61, 20, 74, 86, 66, 82, 79, 80, 31, 17, 10, 75, 32, 81, 96, 93, 33, 71, 10, 57, 45, 80, 54, 75, 32, 45, 22, 92, 26, 86, 33, 43, 51, 18, 21, 41, 19, 0, 11, 14}, {67, 72, 66, 6, 30, 85, 86, 19, 55, 16, 61, 20, 4, 30, 80, 87, 36, 62, 56, 63, 71, 77, 69, 14, 34, 47, 11, 11, 74, 31, 92, 16, 57, 87, 14, 15, 28, 51, 81, 79, 88, 21, 26, 10, 76, 10, 75, 37, 37, 36, 64, 12, 45, 11}, {79, 68, 3, 73, 64, 35, 59, 74, 71, 40, 98, 49, 74, 32, 88, 67, 76, 21, 38, 26, 60, 49, 28, 53, 96, 39, 39, 59, 29, 58, 89, 33, 76, 43, 90, 34, 68, 86, 63, 30, 31, 44, 50, 83, 22, 53, 72, 40, 84, 69, 43, 83, 27, 21}, {41, 3, 99, 51, 2, 71, 89, 18, 2, 63, 88, 97, 42, 96, 98, 39, 33, 19, 73, 66, 20, 35, 22, 21, 60, 51, 12, 94, 20, 84, 99, 89, 33, 48, 83, 58, 89, 41, 3, 83, 41, 92, 58, 43, 60, 41, 84, 67, 66, 16, 28, 32, 62, 75}, {71, 48, 86, 64, 81, 98, 33, 98, 84, 21, 50, 7, 64, 17, 22, 3, 59, 2, 8, 85, 18, 49, 9, 28, 21, 25, 72, 62, 85, 19, 58, 33, 35, 31, 40, 54, 18, 52, 33, 75, 33, 1, 20, 64, 35, 54, 59, 91, 64, 22, 61, 34, 77, 11}, {62, 78, 73, 17, 21, 21, 3, 35, 41, 39, 55, 5, 21, 18, 40, 39, 45, 93, 48, 47, 77, 53, 12, 94, 1, 28, 88, 83, 27, 22, 31, 33, 22, 13, 36, 4, 52, 48, 88, 12, 39, 88, 36, 54, 57, 80, 98, 12, 21, 83, 44, 20, 51, 32}, {70, 41, 47, 2, 86, 66, 63, 38, 27, 99, 44, 90, 22, 89, 34, 32, 96, 6, 55, 20, 84, 31, 29, 39, 29, 48, 69, 45, 73, 38, 69, 82, 54, 75, 91, 89, 62, 90, 24, 13, 18, 41, 27, 42, 63, 97, 37, 40, 20, 33, 78, 92, 32, 8}, {0, 68, 53, 69, 29, 88, 63, 80, 23, 74, 95, 11, 11, 6, 16, 67, 46, 29, 63, 98, 32, 92, 43, 79, 87, 34, 71, 69, 25, 66, 13, 35, 18, 82, 81, 16, 12, 62, 51, 55, 62, 5, 42, 79, 60, 63, 4, 18, 81, 71, 55, 28, 67, 40}, {94, 67, 56, 28, 30, 1, 61, 40, 81, 66, 68, 95, 85, 81, 23, 25, 27, 30, 33, 83, 42, 2, 50, 90, 50, 82, 66, 62, 9, 1, 11, 74, 8, 94, 66, 86, 69, 59, 65, 85, 58, 5, 36, 56, 6, 96, 14, 79, 52, 14, 60, 99, 8, 55}, {60, 38, 30, 32, 92, 56, 86, 3, 49, 94, 34, 67, 87, 9, 95, 37, 3, 77, 46, 67, 80, 52, 40, 28, 77, 14, 18, 29, 68, 62, 11, 75, 71, 15, 76, 3, 31, 46, 5, 16, 2, 17, 74, 23, 2, 1, 33, 69, 57, 96, 79, 48, 64, 77}, {42, 83, 40, 9, 13, 78, 35, 80, 72, 95, 20, 73, 84, 60, 43, 64, 51, 17, 96, 93, 25, 53, 4, 22, 50, 49, 13, 66, 88, 29, 50, 72, 93, 20, 62, 92, 5, 79, 55, 81, 26, 73, 56, 36, 90, 82, 1, 53, 31, 42, 59, 8, 75, 37}, {71, 35, 94, 14, 51, 3, 4, 77, 8, 84, 96, 10, 72, 64, 83, 64, 20, 44, 42, 62, 90, 5, 62, 1, 80, 33, 10, 31, 85, 41, 16, 92, 25, 45, 71, 64, 61, 12, 41, 81, 65, 30, 8, 23, 90, 63, 1, 61, 16, 33, 56, 18, 88, 15}, {8, 30, 44, 18, 48, 72, 76, 48, 97, 29, 10, 99, 22, 3, 21, 36, 86, 32, 34, 58, 52, 1, 27, 27, 32, 99, 85, 10, 3, 85, 64, 25, 89, 64, 70, 62, 28, 89, 17, 77, 72, 61, 52, 58, 94, 51, 88, 11, 34, 46, 42, 40, 53, 53}, {97, 59, 29, 82, 2, 21, 91, 91, 0, 9, 55, 72, 21, 78, 30, 33, 8, 41, 90, 87, 45, 22, 28, 36, 46, 33, 3, 40, 77, 67, 58, 47, 78, 7, 42, 72, 68, 70, 97, 4, 84, 19, 95, 97, 85, 41, 35, 2, 44, 62, 44, 80, 32, 23}, {19, 21, 95, 94, 0, 61, 23, 84, 84, 71, 59, 11, 8, 36, 70, 95, 53, 59, 74, 1, 53, 63, 19, 80, 39, 66, 99, 6, 0, 22, 29, 80, 96, 40, 22, 66, 45, 66, 92, 66, 2, 37, 32, 35, 20, 59, 8, 29, 78, 42, 56, 56, 42, 35}, {97, 87, 89, 91, 55, 12, 76, 28, 69, 6, 42, 23, 5, 2, 53, 26, 21, 42, 85, 53, 44, 39, 36, 83, 86, 99, 85, 83, 67, 77, 92, 76, 97, 13, 34, 47, 33, 16, 22, 47, 91, 96, 11, 40, 90, 4, 94, 93, 13, 60, 64, 51, 37, 32}, {74, 31, 65, 71, 25, 31, 23, 75, 9, 23, 35, 52, 45, 11, 29, 22, 44, 62, 27, 51, 99, 77, 5, 50, 39, 40, 73, 87, 24, 14, 9, 1, 20, 28, 87, 46, 27, 19, 36, 18, 35, 47, 57, 37, 7, 83, 38, 74, 8, 46, 58, 43, 11, 83}, {60, 55, 20, 98, 56, 24, 29, 72, 89, 42, 0, 29, 71, 64, 62, 5, 94, 82, 87, 84, 57, 91, 59, 63, 45, 73, 76, 49, 51, 63, 71, 90, 73, 47, 81, 44, 22, 39, 74, 95, 20, 3, 9, 79, 54, 88, 51, 34, 88, 63, 10, 19, 66, 84}, {34, 63, 6, 30, 87, 22, 74, 8, 94, 22, 73, 51, 46, 13, 55, 55, 11, 1, 56, 24, 34, 41, 26, 67, 5, 46, 3, 9, 70, 59, 72, 36, 79, 31, 69, 76, 36, 14, 43, 10, 44, 15, 92, 57, 7, 21, 91, 37, 33, 16, 79, 65, 13, 0}, {2, 9, 5, 57, 27, 21, 53, 81, 44, 61, 79, 35, 26, 25, 1, 84, 95, 33, 94, 21, 90, 15, 64, 65, 55, 9, 92, 93, 14, 55, 65, 38, 30, 1, 58, 55, 71, 6, 42, 60, 97, 97, 65, 86, 28, 60, 5, 1, 77, 84, 92, 40, 55, 8}, {65, 68, 46, 74, 87, 40, 73, 25, 65, 38, 15, 28, 13, 7, 81, 7, 20, 33, 63, 32, 66, 70, 81, 90, 99, 36, 89, 91, 65, 15, 30, 39, 35, 23, 61, 73, 63, 1, 44, 94, 3, 41, 91, 85, 29, 31, 27, 50, 49, 30, 3, 4, 14, 93}, {70, 85, 45, 85, 83, 76, 74, 76, 44, 31, 32, 69, 38, 38, 43, 52, 66, 68, 58, 61, 29, 57, 59, 21, 74, 87, 96, 42, 64, 7, 67, 17, 2, 67, 2, 25, 0, 61, 26, 82, 93, 57, 25, 59, 55, 88, 44, 37, 40, 27, 26, 34, 46, 28}, {51, 59, 45, 28, 32, 66, 90, 29, 56, 84, 28, 2, 2, 63, 35, 14, 74, 64, 80, 90, 54, 83, 88, 20, 13, 92, 78, 1, 75, 63, 88, 98, 52, 59, 82, 74, 43, 36, 77, 72, 39, 99, 39, 72, 21, 59, 76, 85, 73, 31, 67, 51, 67, 68}, {75, 90, 7, 84, 40, 35, 25, 36, 82, 46, 74, 58, 47, 70, 56, 68, 18, 73, 45, 94, 40, 8, 47, 98, 80, 64, 32, 99, 20, 50, 25, 15, 62, 37, 89, 59, 9, 70, 15, 58, 88, 5, 30, 96, 73, 15, 31, 45, 12, 63, 93, 89, 95, 7},
                {92, 36, 62, 47, 58, 8, 76, 1, 94, 3, 60, 6, 12, 39, 2, 25, 63, 8, 90, 70, 68, 14, 60, 1, 63, 79, 99, 44, 83, 60, 0, 15, 4, 32, 30, 79, 64, 54, 63, 67, 59, 57, 77, 5, 35, 30, 27, 52, 96, 22, 77, 62, 71, 69}};

//        StringUtil.printObject(a[0]);//y=0
        long time1 = System.currentTimeMillis();
        int i = longestIncreasingPath(a);
        long time2 = System.currentTimeMillis();
        System.out.println("usedTime:" + (time2 - time1));
        StringUtil.printObject(i);
    }

    //从这个点能到多个点
    Map<String, List<String>> map = new HashMap<>();

    //使用第二种算法
    public int longestIncreasingPath(int[][] matrix) {
        if (null == matrix || 0 == matrix.length || 0 == matrix[0].length) {
            return 0;
        }
        long time1 = System.currentTimeMillis();
        handleMap(matrix);
        long time2 = System.currentTimeMillis();
        List<String> roots = getRoots();
        if (null == roots || 0 == roots.size()) {
            return 1;
        }
        long time3 = System.currentTimeMillis();
        List<List<String>> allPaths = getPath(roots);
        long time4 = System.currentTimeMillis();
        int max = 0;
        for (List<String> paths : allPaths) {
            max = Math.max(max, paths.size());
//            StringUtil.printObject(paths);
        }
        long time5 = System.currentTimeMillis();
        System.out.println("handleMapTime:" + (time2 - time1) + " getRootsTime:" + (time3 - time2) + " getPath:" + (time4 - time3) + " getMaxTime:" + (time5 - time4));
        return max;
    }

    private List<List<String>> getPath(List<String> roots) {
        List<List<String>> result = new ArrayList<>();
        //初始化
        for (int i = 0; i < roots.size(); i++) {
            List<String> temp = new ArrayList<>();
            temp.add(roots.get(i));
            result.add(temp);
        }

        doGetPath(result);
        return result;
    }

    private void doGetPath(List<List<String>> allPaths) {
        for (int now = 0; now < allPaths.size(); now++) {
            List<String> currentPaths = allPaths.get(now);
            String lastPoint = currentPaths.get(currentPaths.size() - 1);//最后一个点
            List<String> nextPoints = map.get(lastPoint);
            if (null == nextPoints) {//继续获取下一个path的全路径
                continue;
            } else {//获得了这个点能到的下一个点的列表
                do {
                    if (1 == nextPoints.size()) {//只有一个下节点，则直接添加到本path中
                        currentPaths.add(nextPoints.get(0));
                    } else {//有多个下节点，需要拷贝当前路径（n-1个），并添加到result中
                        for (int j = 1; j <= nextPoints.size() - 1; j++) {
                            List<String> tempPaths = new ArrayList<>();
                            for (String currentPath : currentPaths) {//拷贝路径
                                tempPaths.add(currentPath);
                            }
                            tempPaths.add(nextPoints.get(j));
                            allPaths.add(tempPaths);
                        }
                        //第一个path单独加当新节点
                        currentPaths.add(nextPoints.get(0));
                    }
                    nextPoints = map.get(currentPaths.get(currentPaths.size() - 1));
                } while (null != nextPoints && 0 != nextPoints.size());
            }
        }
    }

    private List<String> getRoots() {
        List<String> points = new ArrayList<>();
        Set<String> keySet = map.keySet();
        Collection<List<String>> values = map.values();
        Set<String> allNextPoint = new HashSet<>();
        values.forEach(v -> {
            if (null != v) {
                allNextPoint.addAll(v);
            }
        });
        //如何获取根元素，就是从key中找到没有被指向的，只有key指向其他的节点
        keySet.forEach(key -> {
            if (!allNextPoint.contains(key)) {//表明这个key是最大值的点
                points.add(key);
            }
        });
        return points;
    }

    private void handleMap(int[][] matrix) {
        int yLength = matrix.length;
        int xLength = matrix[0].length;
        for (int x = 0; x < xLength; x++) {
            for (int y = 0; y < yLength; y++) {
                boolean top = top(matrix, x, y);
                if (top) {
                    putMap(x + "," + y, x + "," + (y - 1));
                }
                boolean left = left(matrix, x, y);
                if (left) {
                    putMap(x + "," + y, (x - 1) + "," + y);
                }
                boolean down = down(matrix, x, y);
                if (down) {
                    putMap(x + "," + y, x + "," + (y + 1));
                }
                boolean right = right(matrix, x, y);
                if (right) {
                    putMap(x + "," + y, (x + 1) + "," + y);
                }
            }
        }
    }

    private void putMap(String startPoint, String nextPoint) {
        List<String> paths = map.get(startPoint);
        if (null == paths) {
            paths = new ArrayList<>();
            paths.add(nextPoint);
            map.put(startPoint, paths);
        } else {
            paths.add(nextPoint);
        }
    }


    private boolean right(int[][] matrix, int x, int y) {
        if (x + 1 >= matrix[0].length) {
            return false;
        }
        if (matrix[y][x] > matrix[y][x + 1]) {
            return true;
        }
        return false;
    }

    private boolean down(int[][] matrix, int x, int y) {
        if (y + 1 >= matrix.length) {
            return false;
        }
        if (matrix[y][x] > matrix[y + 1][x]) {
            return true;
        }
        return false;
    }

    private boolean left(int[][] matrix, int x, int y) {
        if (x - 1 < 0) {
            return false;
        }
        if (matrix[y][x] > matrix[y][x - 1]) {
            return true;
        }
        return false;
    }

    //判断当前节点是否比上面节点大，大表示能通过
    private boolean top(int[][] matrix, int x, int y) {
        if (y - 1 < 0) {
            return false;
        }
        if (matrix[y][x] > matrix[y - 1][x]) {
            return true;
        }
        return false;
    }
    public int longestIncreasingPath3(int[][] matrix) {
        if (matrix == null || matrix.length == 0 || matrix[0] == null || matrix[0].length == 0) return 0;
        int row = matrix.length;
        int col = matrix[0].length;

        int[][] dp = new int[row][col];
        int max = 0;

        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                max = Math.max(max, search2(matrix, Integer.MIN_VALUE, i, j, dp));
            }
        }
        return max;
    }

    private int search2(int[][] matrix, int minValue, int i, int j, int[][] dp) {
        return 0;
    }

    /**
     * 官方最佳答案
     */
    public int longestIncreasingPath2(int[][] matrix) {
        if (matrix == null || matrix.length == 0 || matrix[0] == null || matrix[0].length == 0) return 0;
        int row = matrix.length;
        int col = matrix[0].length;

        int[][] dp = new int[row][col];
        int max = 0;

        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                max = Math.max(max, search(matrix, Integer.MIN_VALUE, i, j, dp));
            }
        }
        return max;
    }

    /**
     * @param matrix 源数据
     * @param curNum 当前位置的值
     * @param i      下一个位置的y值
     * @param j      下一个位置的x值
     * @param dp     记录该位置最大长度
     * @return 最大路径长度
     */
    private int search(int[][] matrix, int curNum, int i, int j, int[][] dp) {
        int row = matrix.length;
        int col = matrix[0].length;
        if (i < 0 || i >= row || j < 0 || j >= col || matrix[i][j] <= curNum) return 0;
        if (dp[i][j] != 0) return dp[i][j];

        int max = 0;
        max = Math.max(max, search(matrix, matrix[i][j], i - 1, j, dp));//上
        max = Math.max(max, search(matrix, matrix[i][j], i + 1, j, dp));//下
        max = Math.max(max, search(matrix, matrix[i][j], i, j - 1, dp));//左
        max = Math.max(max, search(matrix, matrix[i][j], i, j + 1, dp));//右
        dp[i][j] = max + 1;
        return max + 1;
    }
}
